package com.ruoyi.common.enums.business;


import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum GenderEnum {
    MALE(0, "男"),
    FEMALE(1, "女"),
    UNKNOWN(2, "未知");

    private final Integer code;
    private final String desc;

    private static final Map<Integer, GenderEnum> ENUM_MAP;

    static {
        GenderEnum[] values = values();
        Map<Integer, GenderEnum> enToCn = new HashMap<>(values.length);
        for (GenderEnum value : values) {
            enToCn.put(value.code, value);
        }
        ENUM_MAP = Collections.unmodifiableMap(enToCn);
    }

    /**
     * 根据类型查找枚举
     */
    public static GenderEnum getEnumByCode(Integer code) {
        GenderEnum genderEnum = ENUM_MAP.get(code);
        if (ObjectUtil.isEmpty(genderEnum)) {
            throw new RuntimeException("");
        }
        return genderEnum;
    }


}
